<?php

/**
 * index
 * @author 刘军 
 * @version 1.0
 * @package application
 */
class Company extends Front_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('company_model');
        $this->load->model('news_model');
//        $this->load->model('member_company_info_model');
    }

    //基本资料
    public function index() {
        $this->load->model('team_member_model');
        $search['limit'] = array('persize' => 5, 'offset' => 1);
        $fields = array('id', 'name', 'photo_url');
        $data['teamMember'] = $this->team_member_model->get_appoint_values($fields, null, $search);
          
        $id = 1;
        $data['companyInfo'] = $this->company_model->get_by_pk($id);
        $this->load->view($this->_site_path . '/company/index', $data);
    }

    //联系方式
    public function contact() {
        $id = 6;
        $data['contact'] = $this->company_model->get_by_pk($id);
        $this->load->view($this->_site_path . '/company/contact', $data);
    }

    //联系方式
    public function single($id) {
        $data['info'] = $this->company_model->get_by_pk($id);
        $this->load->view($this->_site_path . '/company/single', $data);
    }

    //修改基本资料
    public function _update_base_info($infos) {

        //验证规则
        $rule_config = array();
        if ($infos['password']) {
            $rule_config[] = array('field' => 'oldpwd', 'label' => '原密码', 'rules' => 'required');
            $rule_config[] = array('field' => 'password', 'label' => '密码', 'rules' => 'required|min_length[6]|matches[passconf]|md5');
            $rule_config[] = array('field' => 'passconf', 'label' => '确认密码', 'rules' => 'required');
        }
        //不设安全问题，问题答案清空
        intval($infos['question_id']) ? $rule_config[] = array('field' => 'answer', 'label' => '安全问题答案', 'rules' => 'required') : $infos['answer'] = '';
        $this->load->library('form_validation');
        if ($rule_config) {
            $this->form_validation->set_rules($rule_config);
            if ($this->form_validation->run()) {
                //验证原密码是否正确
                $total = $this->member_model->total(array('password' => md5($infos['oldpwd'])));
                if (!$total) {
                    $this->error_js('原密码不正确！');
                    exit;
                }
            } else {
                $this->error_js('资料有误！');
                exit;
            }
        } else {
            unset($infos['password']);
        }
        //获取session user_id值
        $user_id = $this->session->userdata('user_id');
        if (!$this->member_model->update_by_pk($infos, $user_id)) {
            $this->error_js('基本资料修改失败！');
            exit;
        }
    }

    //修改企业资料
    public function _update_company_info($infos) {

        $pattern = '/^0(\d{3})-\d{7,8}$/';
        if ($infos['tel'] && !preg_match($pattern, $infos['tel'])) {
            $this->error_js('电话号码格式不正确！');
            exit;
        }
        $pattern = '/^1\d{10}$/';
        if ($infos['mobile'] && !preg_match($pattern, $infos['mobile'])) {
            $this->error_js('手机格式不正确！');
            exit;
        }
        //获取session user_id值
        $user_id = $this->session->userdata('user_id');
        $infos['industry_id'] = $infos['industry_id'][1] ? $infos['industry_id'][1] : $infos['industry_id'][0];
        $infos['member_id'] = $user_id;
        $infos['created'] = $infos['modified'] = time();
        //验证该用户member_id是否已经存在
        $id = $this->member_company_info_model->get_value_by_notpk(id, array('member_id' => $user_id));
        //存在则对表进行修改数据
        if ($id) {
            if (!$this->member_company_info_model->update_by_pk($infos, $id)) {
                $this->error_js('企业资料修改失败！');
                exit;
            }
            //不存在则对表进行增加数据
        } else {
            $id = $this->member_company_info_model->insert($infos);
            if (!$id) {
                $this->error_js('企业资料修改失败！');
                exit;
            }
        }
        $this->_img_upload($id);
    }

    //图片上传
    public function _img_upload($id) {

        $config['upload_path'] = './uploads/logo/';
        $config['file_name'] = $id;
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1024';
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        if ($this->upload->do_upload('logo')) {
            $data = $this->upload->data();
            if ($data['raw_name'] != $id) {
                $file_name = $config['upload_path'] . $id . $data['file_ext'];
                copy($data['full_path'], $file_name);
                @unlink($data['full_path']);
                if (!$this->member_company_info_model->get_value_by_pk($id, 'logo')) {
                    $this->member_company_info_model->update_by_pk(array('logo' => substr($file_name, 1)), $id);
                }
            }
        }
    }

    //获取企业资料信息
    public function get_company_info() {

        $this->load->model('province_model');
        $this->load->model('member_company_industry_model');
        $data = array();
        //获取session user_id值
        $user_id = $this->session->userdata('user_id');
        $attributes = array('member_id' => $user_id);
        //企业资料信息
        $data['company_info'] = $this->member_company_info_model->get_by_attributes($attributes);
        $data['company_info']['unit_name'] = $this->member_model->get_value_by_pk($user_id, 'unit_name');
        //获取行业父类ID
        if ($data['company_info']['industry_id']) {
            $pid = $this->member_company_industry_model->get_value_by_pk($data['company_info']['industry_id'], 'parent_id');
            $data['company_info']['industry_pid'] = $pid ? $pid : $data['company_info']['industry_id'];
        }
        //所有省
        $data['provinces'] = $this->province_model->get_values('id', 'province_name');
        //公司规模
        $data['company_scale'] = config_item('company_scale');
        //公司行业分类
        $data['company_industry'] = $this->member_company_industry_model->get_values('id', 'industry_name', array('parent_id' => '0'));
        //p($data);exit;
        $this->load->view_part($this->_site_path . '/company/get_company_info', $data);
    }

    //获取分类
    public function get_category() {

        $pid = intval($_GET['pid']);
        $tablename = $_GET['tablename'];
        $id = intval($_GET['id']);
        if ($pid <= 0 || !is_string($tablename)) {
            echo '<option value="0" selected="selected">--请选择--</option>';
            exit;
        }
        switch ($tablename) {
            case 'member_company_industry':
                $category_name = 'industry_name';
                break;
            case 'city':
                $category_name = 'city_name';
                break;
            case 'county':
                $category_name = 'county_name';
                break;
            default:
                echo '<option value="0" selected="selected">--请选择--</option>';
                exit;
        }
        $this->load->model($tablename . '_model', $tablename);
        $data = array();
        $data['id'] = $id;
        $data['categories'] = $this->$tablename->get_values('id', $category_name, array('parent_id' => $pid));
        if (!$data['categories']) {
            echo '<option value="0" selected="selected">--请选择--</option>';
            exit;
        }
        $this->load->view_part($this->_site_path . '/company/ajax_get_category', $data);
    }

}
